package com.enjoyor.health.HGPT.core.config;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaHttpMethod;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.spring.SpringMVCUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import static cn.dev33.satoken.exception.NotLoginException.DEFAULT_MESSAGE;

/**
 * @author Zhu Min
 * @date 2023/11/8 15:46
 */
@Slf4j
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    @Autowired
    private ParamConfig paramConfig;

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/app/user/login",
                        "/app/user/logout",
                        "/app/user/sendSmsCode",

                        "/open/**",

                        "/rest/doLogin",
                        "/js/*","/fonts/*","/css/*","/favicon.ico","/download/*",
                        "/error",
                        "/index.html",
                        "/aiHealthReport/**",
                        "/aiHealthReport",
                        "/aiHealthReportDetail");
    }

    @Bean
    public SaServletFilter getSaServletFilter() {
        return new SaServletFilter()
                .addInclude("/**")
                .addExclude(
                        "/app/user/login",
                        "/app/user/logout",

                        "/open/**",

                        "/sso/*", "/favicon.ico",
                        "/js/*","/fonts/*","/css/*","/favicon.ico","/download/*",
                        "/rest/doLogin",
                "/index.html",
                "/aiHealthReport/**",
                "/aiHealthReport",
                "/aiHealthReportDetail")
//                .setAuth(obj -> {
//                    if(!StpUtil.isLogin()) {
//                        //String back = SaFoxUtil.joinParam(SaHolder.getRequest().getUrl(), SpringMVCUtil.getRequest().getQueryString());
//                        //SaHolder.getResponse().redirect(
//                        //        paramConfig.getReportUrl() + "/index.html" /*+ "?back=/" + back*/);
//                        throw new NotLoginException(DEFAULT_MESSAGE, "-1", "-1");
//                        //SaRouter.back();
//                    }
//                })
                .setBeforeAuth(obj -> {
                    // ---------- 设置跨域响应头 ----------
                    SaHolder.getResponse()
                            // ---------- 设置跨域响应头 ----------
                            // 允许指定域访问跨域资源
                            .setHeader("Access-Control-Allow-Origin", "*")
                            // 允许所有请求方式
                            .setHeader("Access-Control-Allow-Methods", "*")
                            // 允许的header参数
                            .setHeader("Access-Control-Allow-Headers", "*")
                            // 有效时间
                            .setHeader("Access-Control-Max-Age", "3600")
                    ;

                    // 如果是预检请求，则立即返回到前端
                    SaRouter.match(SaHttpMethod.OPTIONS)
                            .free(r -> log.debug("--------OPTIONS预检请求，不做处理"))
                            .back();

                });

    }
}
